﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using ImageAppDataModels;
using ImageAppViewModels;
using WpfImageDuplicates;

namespace FindDuplicates.Plugin
{
    public class DupeRunner
    {
        protected List<Pair<IFileImage, IFileImage>> _dupes;
        protected int _currentDupe;

        public int DupeCount
        {
            get { return _dupes == null ? 0 : _dupes.Count; }
        }
        public void AssignDuplicates(List<Pair<IFileImage, IFileImage>> dupes)
        {
            _dupes = dupes;
            _currentDupe = _dupes.Count - 1;
        }

        public void ShowDupe(CompareImagesWindow compareWindow)
        {
            ShowDupe(compareWindow, _currentDupe);
        }

        public void RemoveAll(FileImageCollectionViewModel viewModel)
        {
            for (; _currentDupe >= 0; _currentDupe--)
            {
                string fileName = _dupes[_currentDupe].obj1.FileName;
                var vm1 = viewModel.Find(fileName);

                if (vm1 != null)
                {
                    viewModel.Delete(vm1);
                }
            }
        }

        public void ShowDupe(CompareImagesWindow compareWindow, int n)
        {
            if (n >= _dupes.Count || n < 0)
            {
                if (compareWindow == null)
                    return;

                try
                {
                    compareWindow.Close();
                }
                catch { }

                return;
            }

            var vm1 = new FileImageViewModel(_dupes[n].obj1 as FileImage, Window1.MainWindow.FilterHandler._currentFilterCollection.Collection, null);
            var vm2 = new FileImageViewModel(_dupes[n].obj2 as FileImage, Window1.MainWindow.FilterHandler._currentFilterCollection.Collection, null);
            var viewModelPair = new Pair<FileImageViewModel, FileImageViewModel>(vm1, vm2);
            compareWindow.FileImageViewModels = viewModelPair;
            compareWindow.Show();
        }

        public void ShowNextDupe(CompareImagesWindow compareWindow)
        {
            if (_currentDupe >= 0)
            {
                _currentDupe--;
                ShowDupe(compareWindow, _currentDupe);
            }
        }

        public void Clear()
        {
            _dupes = null;
            _currentDupe = -1;
        }

        public void RemoveDupe(FileImageCollectionViewModel viewModel)
        {
            string fileName = _dupes[_currentDupe].obj1.FileName;
            var vm1 = viewModel.Find(fileName);

            if (vm1 == null)
                throw new Exception("Could not find " + fileName);

            viewModel.Delete(vm1);
        }
    }
}
